<?php
Class C_family extends Cemvc_Control_Base
{
  private $db;

  public function __construct()
  {
    parent::__construct();
    $this->db = new M_qin_family();


    if(isset($_REQUEST['userid']) && $_REQUEST['userid'])
      {
	$db = new M_qin_users();
	$user = $db->get($_REQUEST['userid']);
	$region = new M_qin_region();
	$user['region'] = $user['region_code'] ? $region->get_full_name($user['region_code']) : "";
	$user['gender_str'] = $user['gender'] == MALE ? '男' : '女';
	$user["figureurl"] = M_Tools::get_figure_url($user["figureurl"], $user["gender"]);
	$this->view->assign("user", $user);
	$this->user = $user;

	if (isset($_SESSION["id"]) && 
	    ($_SESSION["id"] == $user["creator"] ||	/* I create the user */
	     $_SESSION["id"] == $user["id"] ||	/* I am the user */
	     ($_SESSION["creator"] > 0 && $_SESSION["creator"] == $user["creator"]))) /* we were created with a same user */
	  {
	    $this->view->assign("authorized", 1);
	  }
      }
    $this->view->assign("open", M_Options::get_open_state());
  }

  public function init()
  {
    if ($_POST)
      {
	if (!isset($_POST["name"]) || empty($_POST["name"]) ||
	    !isset($_POST["creator"]) || empty($_POST["creator"]) ||
	    !isset($_POST["region_code"]) || empty($_POST["region_code"]) ||
	    !isset($_POST["master"]) || empty($_POST["master"]))
	  $this->view->assign("error", "名称、地区、创建者、户主是必选的。");
	else
	  {
	    $family = $this->db->add($_POST);
	    if (isset($family["id"]) && $family["id"] > 0)
	      {
		$this->redirect("family/manage");
		return;
	      }
	    $this->view->assign("error", "创建家谱失败，请稍后重试。");	    
	  }
      }
    $this->display();
  }

  public function manage()
  {
    $res = $this->db->get_families($_SESSION["id"]);
    if (!is_array($res) || count($res) < 1)
      {
	$this->redirect("family/init");
	return;
      }
    $region = new M_qin_region();
    $families = array();
    foreach($res as $family)
      {
	$family["region_name"] = $family["region_code"] ? $region->get_full_name($family["region_code"]) : "";
	$families[] = $family;
      }
    
    $this->view->assign("families", $families);
    $this->assign("page", $this->db->showPages());
    $this->display();
  }

  public function add()
  {
    $region = new M_qin_region();
    $provinces = $region->getProvinceList();
    $this->view->assign("provinces", M_Tools::make_options($provinces));

    if ($_POST)
      {
	/* var_dump($_POST); */
	/* die(); */
	if (!isset($_POST["name"]) || empty($_POST["name"]) ||
	    !isset($_POST["creator"]) || empty($_POST["creator"]) ||
	    !isset($_POST["region_code"]) || empty($_POST["region_code"]) ||
	    !isset($_POST["realname"]) || empty($_POST["realname"]) ||
	    !isset($_POST["gender"]) || empty($_POST["gender"]))
	  $this->view->assign("error", "名称、地区、创建者、户主是必选的。");
	else
	  {
	    /* create master  */
	    $this->db->mysqlQuery("start transaction");
	    $this->db->query();
	    $user = new M_qin_users();
	    $inst = $user->add($_POST);

	    /* create family */
	    if ($inst && $inst["id"] > 0)
	      {
		$para = $_POST;
		$para["master"] = $inst["id"];
		$family = $this->db->add($para);
		if (isset($family["id"]) && $family["id"] > 0)
		  {
		    $this->db->mysqlQuery("commit");
		    $this->db->query();

		    $this->redirect("family/add_complete?fami_id=".$family["id"]);
		    return;
		  }
	      }
	    $this->db->mysqlQuery("rollback");
	    $this->db->query();
	    $this->view->assign("error", "创建家谱失败，请稍后重试。");
	  }
	$this->view->assign("post", $_POST);	
	$options = $region->get_selected_options($_POST["region_code"]);
	$this->view->assign("provinces", $options["provinces"]);
	$this->view->assign("cities", $options["cities"]);
	$this->view->assign("towns", $options["towns"]);
      }

    /* $this->view->assign("creator", $_SESSION["id"]); */
    $this->display();
  }

  public function add_complete()
  {
    if (!isset($_REQUEST["fami_id"]) || empty($_REQUEST["fami_id"]))
      die("you must specified a family id");

    $this->view->assign("fami_id", $_REQUEST["fami_id"]);
    $this->display();
  }

  public function view()
  {
    if (!isset($_REQUEST["fami_id"]) || empty($_REQUEST["fami_id"]))
      die("you must specified a family id");

    $fami = $this->db->get($_REQUEST["fami_id"]);
    $region = new M_qin_region();
    $fami["region_name"] = $fami["region_code"] ? $region->get_full_name($fami["region_code"]) : "";
    $this->view->assign("family", $fami);
    $this->display();
    
  }
  public function edit()
  {
    if ($_POST)
      {
	if (isset($_POST["id"]) && $_POST["id"] > 0)
	  {
	    print_r($_POST);
	    /* die(); */
	    $para = $_POST;
	    unset($para["id"]);
	    $this->db->update_family($_POST["id"], $para);
	    
	    $this->redirect("family/update_complete");
	    return;
	  }
      }

    if (!isset($_REQUEST["fami_id"]) || empty($_REQUEST["fami_id"]))
      die("you must specified a family id");

    $fami = $this->db->get($_REQUEST["fami_id"]);
    $region = new M_qin_region();
    $options = $region->get_selected_options($fami["region_code"]);
    $this->view->assign("provinces", $options["provinces"]);
    $this->view->assign("cities", $options["cities"]);
    $this->view->assign("towns", $options["towns"]);
    $this->view->assign("family", $fami);
    $this->display();
  }

  public function update_complete()
  {
    $this->display();
  }
}
